import 'package:development_assistant/config/colors.dart';
import 'package:development_assistant/controller/test_controller.dart';
import 'package:development_assistant/ui/screen/widget/custom_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../config/font.dart';

class AutoTest extends StatelessWidget {

  final TestController testController=Get.put(TestController());

  AutoTest({super.key});

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: _appBar(),
      resizeToAvoidBottomInset: false,
      bottomNavigationBar: _startTest(),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.only(top: 20.h,left: 20.w,right: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _envFile(),
              SizedBox(height: 10.w,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _selectEnv(),
                  _testField("测试线程".tr, testController.testThread)
                ],
              ),
              SizedBox(height: 10.w,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _testField("间隔时长".tr, testController.testInterval),
                  _testField("循环次数".tr, testController.testCycles),
                ],
              ),
              _testStep()
            ],
          ),
        ),
      ),
    );
  }

  _startTest(){
    return Container(
      width: 200.w,
      height: 35.h,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 100.w),
      child: ElevatedButton(
        onPressed: (){
          Get.offNamed("/TestResult");
        },
        style: ElevatedButton.styleFrom(
            backgroundColor:ColorsConfig().buttonColorOfTheme,
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20)
            )
        ),
        child: Text("启动测试".tr,style: TextStyle(fontWeight: FontWeight.bold),),
      ),
    );
  }

  _envFile(){
    return SizedBox(
      width: 320.w,
      height: 40.h,

      child: Center(
        child: TextField(
          maxLines: 1,
          decoration: InputDecoration(
              hintText:"自动化测试名称".tr,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){
            testController.testName.value=e;
          },
        ),
      ),
    );
  }

  _testField(String hintText,RxInt testValue){
    return SizedBox(
      width: 150.w,
      height: 40.h,
      child: Center(
        child: TextField(
          maxLines: 1,
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){
            if(e!=""){
              testValue.value=int.parse(e);
            }
          },
        ),
      ),
    );
  }

  _testStep(){
    return Column(
      children: [
        //添加按钮
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text("测试步骤".tr,style: FontConfig.fieldTitle,),
            Transform.scale(
              scale: 0.7,
              child: ElevatedButton.icon(
                onPressed: (){
                  testController.clear();
                  Get.toNamed("/AddApi");
                },
                icon: const Icon(Icons.add),
                label: Text("添加".tr),
                style: ElevatedButton.styleFrom(
                    backgroundColor: ColorsConfig().buttonColorOfTheme,
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    )
                ),
              ),
            )
          ],
        ),
        //参数列表
        Obx(() => Wrap(
          children: List<Widget>.generate(testController.testApi.length, (index) {
            return Container(
              height: 70.h,
              margin: EdgeInsets.only(bottom: 10.h),
              padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 15.h),
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                  border: Border.all(color:Colors.grey,width: 1)
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Container(
                    width: 45.w,
                    height: 45.w,
                    margin: EdgeInsets.symmetric(horizontal: 10.w),
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(50),
                        color: ColorsConfig.methodTagColors[testController.testApi[index].apiMethod]
                    ),
                    child: Center(child: Text(testController.testApi[index].apiMethod,style: TextStyle(color: Colors.white),)),
                  ),
                  SizedBox(
                    width: 190.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        CustomText(text: testController.testApi[index].apiName, textStyle: FontConfig.apiName),
                        const SizedBox(height: 5,),
                        CustomText(text: testController.testApi[index].apiUrl, textStyle: FontConfig.urlStyle)
                      ],
                    ),
                  ),
                  IconButton(
                      onPressed: (){
                        testController.testApi.removeAt(index);
                        testController.testBody.removeAt(index);
                        testController.testParams.removeAt(index);
                        testController.testHeaders.removeAt(index);
                      },
                      icon: const Icon(Icons.delete_outline)
                  )
                ],
              ),
            );
          }),
        ))
      ],
    );
  }

  _selectEnv(){
    return Container(
      width: 150.w,
      height: 40.h,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          border: Border.all(color: Colors.grey,width: 1)
      ),
      child: Center(
        child: DropdownButton(
          hint: Obx(()=>Text(testController.testEnv.value.tr)),
          items: <String>['测试环境'.tr, '正式环境'.tr, '体验环境'.tr,'云端环境'.tr].map((String value) {
            return DropdownMenuItem<String>(
                value: value,
                child: SizedBox(
                  width: 120,
                  child: Text(value,style: const TextStyle(fontSize: 17),),
                )
            );
          }).toList(),
          onChanged: (e){
            testController.testEnv.value=e!;
          },
          underline: Container(width: 0),
        ),
      ),
    );
  }

  _appBar(){
    return AppBar(
      title: Text("自动化测试".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          Get.offNamed("/Home",arguments: {"selectIndex":2});
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }
}
